package com.xx.auth.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
@Slf4j
public class SwaggerConfig {
    @Bean
    public Docket api() {
        List<Parameter> pars = getParameters();
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xx"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("小希任务执行器API文档")
                .description("Restful风格的接口")
                .version("1.0")
                .build();
    }

    private List<Parameter> getParameters() {
        List<Parameter> pars = new ArrayList<Parameter>();

        //app端请求头配置
        ParameterBuilder appTokenKey = new ParameterBuilder();
        appTokenKey.name("token").description("授权token名")//name表示名称，description表示描述
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false)
                .defaultValue("")
                .build();//required表示是否必填，defaultValue表示默认值
        pars.add(appTokenKey.build());//添加完此处一定要把下边的带***的也加上否则不生效
        return pars;
    }
}
